<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>字符串操作</title>
</head>
<body>
<script>
    var myString = 'The event will be in May, the 21st of June';
    var myCleanedUpString = myString.replace('May', 'June');
    document.write(myCleanedUpString);

    myString = 'Beginning JavaScript, Beginning Java, Professional JavaScript';
    alert(myString.search('Java'));

    myString = '1997, 1998, 1999, 2000, 2000, 2001, 2002';
    var myMatchArray = myString.match('2000');
    alert(myMatchArray.length);

    var myRegExp = /\b'|'\b/;
    document.write(myRegExp.toString());
    myRegExp = new RegExp("\\b'|'\\b");
    document.write(myRegExp.toString());

    myString = 'Paul, Paula, Pauline, paul, Paul';
    myRegExp = /Paul/;
    myString = myString.replace(myRegExp, 'Ringo');
    alert(myString);

    myRegExp = /Paul/gi;
    myString = myString.replace(myRegExp, 'Ringo');
    alert(myString);

    myRegExp = /\d-\d\d\d-\d\d\d-\d\d\d\d/;
    alert(myRegExp);
    myRegExp = /\d-/;
    alert(myRegExp);
    myRegExp = /\d-\d{3}/;
    alert(myRegExp);
    myRegExp = /\d-\d{3}-\d{3}/;
    alert(myRegExp);
    myRegExp = /\d-\d{3}-\d{3}-\d{4}/;
    alert(myRegExp);

    myRegExp = /Paula?/;
    alert(myRegExp);

    myString = 'Hello world!, let\'s look at boundaries said 007.';
    myRegExp = /\b/g;
    myString = myString.replace(myRegExp, '|');
    alert(myString);

    myString = 'Hello world!, let\'s look at boundaries said 007.';
    myRegExp = /\B/g;
    myString = myString.replace(myRegExp, '|');
    alert(myString);

    myString = 'Paul, Paula, Pauline, paul, Paul';
    myRegExp = /Paul\W/gi;
    myString = myString.replace(myRegExp, 'Ringo');
    alert(myString);

    myString = 'Paul, Paula, Pauline, paul, Paul';
    myRegExp = /Paul\b/gi;
    myString = myString.replace(myRegExp, 'Ringo');
    alert(myString);

    myString = 'Paul, Paula, Pauline, paul, Paul, JeanPaul';
    myRegExp = /\bPaul\b/gi;
    myString = myString.replace(myRegExp, 'Ringo');
    alert(myString);

    myString = 'JavaScript, VBScript and PHP';
    myRegExp = /\b(VB)?(Java)?Script\b/gi;
    myString = myString.replace(myRegExp, 'xxxx');
    alert(myString);

    myString = 'JavaScript, VBScript and PHP';
    myRegExp = /\b(VB|Java)Script\b/gi;
    myString = myString.replace(myRegExp, 'xxxx');
    alert(myString);

    myString = '007,007,001,002,002,003,002,004';
    myRegExp = /(\d+),\1/g;
    myString = myString.replace(myRegExp, 'ERROR');
    alert(myString);

    var myListString = 'apple, bnanan, peach, orange';
    var myFruitArray = myListString.split(', ');
    document.write(myFruitArray.toString());

    myRegExp = /(\d)(\w)/g;
    document.write(myRegExp.toString());
    myString = '2012, 2013, 2014';
    myRegExp = /\d{4}/g;
    document.write(myRegExp.toString());
    myRegExp = /(\d{4})/g;
    myString = myString.replace(myRegExp, 'the year $1');
    document.write(myString);

    myString = 'He then said \'My Name is O\'Connerly, yes that\'s right, O\'Counnerly';
    myRegExp = /'/;
    document.write(myRegExp.toString());
    myRegExp = /\B'|'\B/g;
    myString = myString.replace(myRegExp, '"');
    document.write(myString);

    myString = 'Beginning JavaScript, Beginning Java 2, Professional JavasScript';
    myRegExp = /\bJava\b/i;
    alert(myString.search(myRegExp));

    myString = 'The years were 2012, 2013 and 2014';
    myRegExp = /\b\d{4}\b/g;
    var resultsArray = myString.match(myRegExp);
    if (resultsArray) {
        for (var i = 0; i < resultsArray.length; i++) {
            alert(resultsArray[index]);
        }
    }

    myRegExp = /[a-z]/;
    document.write(myRegExp.toString());
    myRegExp = new RegExp('[a-z]');
    document.write(myRegExp.toString());
    myRegExp = new RegExp('\\b');
    document.write(myRegExp.toString());
    myRegExp = new RegExp('Hello\\b', 'gi');
    document.write(myRegExp.toString());
    myRegExp = new RegExp('Hello\\b', 'i');
    document.write(myRegExp.toString());
    myRegExp = new RegExp('Hello\\b', 'g');
</script>
</body>
</html>